package org.dxy.infrastructure.persistent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import org.dxy.infrastructure.persistent.po.query.ProductViewPo;
import org.dxy.infrastructure.persistent.mapper.ProductViewMapper;
import org.dxy.infrastructure.persistent.service.ProductViewService;
import org.springframework.stereotype.Service;

/**
 * 产品查询视图服务实现类
 */
@Service
public class ProductViewServiceImpl extends MPJBaseServiceImpl<ProductViewMapper, ProductViewPo> implements ProductViewService {
    @Override
    public Long findIdByCode(String productCode) {
        MPJLambdaQueryWrapper<ProductViewPo> lambdaQueryWrapper = new MPJLambdaQueryWrapper<>();
        lambdaQueryWrapper.select(ProductViewPo::getId)
                .eq(ProductViewPo::getProductCode, productCode);
        Long id = this.selectJoinOne(Long.class,lambdaQueryWrapper);

        if (id == null) {
            throw new IllegalArgumentException("产品编码不存在：" + productCode);
        }
        return id;
    }

    @Override
    public ProductViewPo getByCode(String productCode) {
        MPJLambdaQueryWrapper<ProductViewPo> lambdaQueryWrapper = new MPJLambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(ProductViewPo::getProductCode, productCode);
        return this.getOne(lambdaQueryWrapper);
    }
}